package org.opens.mybatisplus.controller;

import lombok.Data;
import org.opens.util.R;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/api")
public class RestfulController {

    /**
     * 功能: 最简单的路径传参api
     */
    @RequestMapping(value = "/restful", method = RequestMethod.GET)
    public Map<String ,Object> getRequest2(
            @RequestParam(value = "id") String id
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("request", "getMethod");
        return map;
    }

    /**
     * 说明:
     * <pre>
     *     1. 这种使带正则的路径传参, 限制后面串的参数必须是至少一位的数字, 否则会报错404.
     * </pre>
     */
    @RequestMapping(value = "/restful/{id:\\d+}", method = RequestMethod.GET)
    public Map<String, Object> getRequest(@PathVariable(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("request", "getMethod");
        return map;
    }

    @RequestMapping(value = "/restful", method = RequestMethod.POST)
    public Map<String, Object> postRequest(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("request", "post");
        return map;
    }

    @RequestMapping(value = "/restful", method = RequestMethod.DELETE)
    public Map<String, Object> deleteRequest(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("request", "delete");
        return map;
    }

    @RequestMapping(value = "/restful", method = RequestMethod.PUT)
    public Map<String, Object> putRequest(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("request", "put");
        return map;
    }

    @RequestMapping(value = "/restful", method = RequestMethod.PATCH)
    public Map<String, Object> patchRequest(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("request", "patch");
        return map;
    }

    @RequestMapping(value = "/restful/json", method = RequestMethod.PATCH)
    public Map<String, Object> patchJsonRequest(
            @RequestBody WebParam webParam
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("webParam", webParam);
        map.put("request", "patch");
        return map;
    }

    @RequestMapping(value = "/restful/json", method = RequestMethod.PUT)
    public Map<String, Object> putJsonRequest(
            @RequestBody WebParam webParam
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("webParam", webParam);
        map.put("request", "put");
        return map;
    }

    @GetMapping(value = "/restful/array")
    public R getWithArray(
            @RequestParam(value = "arr[]") String[] arr
    ) {
        R success = R.success("请求成功");
        success.put("webParam", arr);
        return success;
    }

    @RequestMapping(value = "/zoos/{zoo}", method = RequestMethod.GET)
    public Map<String, Object> getAllZoo(@PathVariable(value = "zoo") Integer zoo) {
        Map<String, Object> result = new HashMap<>();
        result.put("data", zoo);
        return result;
    }

    @Data
    public static class WebParam {

        private Integer id;

        private String name;

        private Date startDate;

    }
}
